package app.modules.security.persistence.dao;

import app.modules.security.dto.web.permission.WebPermissionDTO;
import app.modules.security.persistence.mapper.FunctionPermissionRelationMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jasmine.framework.database.mybatisplus.dao.BaseEntityDAO;
import jasmine.security.rbac.model.SecFunctionPermissionRelation;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author mh.z
 */
@Repository
public class FunctionPermissionRelationDAO
        extends BaseEntityDAO<FunctionPermissionRelationMapper, SecFunctionPermissionRelation> {

    /**
     * 查询指定功能的所有已分配权限
     *
     * @param functionId
     * @return
     */
    public List<WebPermissionDTO> listAssignedPermissions(Long functionId) {
        return baseMapper.listAssignedPermissions(functionId);
    }

    /**
     * 删除指定功能的所有权限分配关系
     *
     * @param functionId
     */
    public void deleteByFunctionId(Long functionId) {
        LambdaQueryWrapper<SecFunctionPermissionRelation> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SecFunctionPermissionRelation::getFunctionId, functionId);

        baseMapper.delete(wrapper);
    }

    /**
     * 删除指定权限的所有功能分配关系
     *
     * @param permissionId
     */
    public void deleteByPermissionId(Long permissionId) {
        LambdaQueryWrapper<SecFunctionPermissionRelation> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SecFunctionPermissionRelation::getPermissionId, permissionId);

        baseMapper.delete(wrapper);
    }

}
